home *** CD-ROM | disk | FTP | other *** search
INI File | 2001-09-10 | 6.4 KB | 209 lines |
- [Name]
- TrigMath v1.0 - Provides events Trig math functions
- By Matthew Peterson, matthew@pinoko.berkeley.edu
-
- [Description]
- 2-19-2000
- This behavior offers the following eight functions (Scroll down for a better
- description of this behavior):
-
- --------------------
- To use this behavior: make a new sprite and add this behavior to it.
- It doesn't matter what image the sprite has (small non-vector images
- are best) nor does it matter where the sprite is placed.
- This sprite will be hidden from view as it is just used as a holder
- for the following math routines:
-
- Trig : This event calculates the Sin, Cos and Tan for input TrigAngle.
- Input - TrigAngle
- Output - sine, cosine, tangent
-
- ArcTan : This event performs the Arc Tangent operation.
- Input - TrigInput
- Output -TrigOutput
-
- Polar2Cart : This event converts polar to cartesian coordinates.
- Input - TrigAngle, TrigDistance
- Output - Trig1X, Trig1Y
-
- Cart2Polar : This event converts cartesian to polar coordinates.
- Input - Trig1X, Trig1Y,
- Trig2X, Trig2Y
- Output - TrigAngle, TrigDistance
-
- Deg2Rad : This event converts Degrees to Radians.
- Input - TrigInput
- Output - TrigOutput
-
- Rad2Deg : This event converts Radians to Degrees.
- Input - TrigInput
- Output - TrigOutput
-
- Sqrt : This event performs the Square root operation.
- Input - TrigInput
- Output - TrigOutput
-
- Modulus : This event performs Output = Input MOD TrigMod.
- Input - TrigInput TrigMod
- Output - TrigOutput
-
- [Parameters]
-
- [Frame Loaded]
- GlobalVars TrigMod
- TrigMod = 360 //default value for angles.
- SpriteOfID($ThisSpriteID).SetVisible(False)
- SpriteOfID($ThisSpriteID).moveto(-1000,-1000)//off stage!
-
- [200000 Trig]
- GlobalVars TrigAngle, sine, cosine, tangent
- //This function calculates the standard 3 trig functions sine,cosine and tangent
- //It does so by rotating the sprite and comparing the new width and height.
- Stretch(-200,-200,-100,-200,-100,-199,-200,-199)
- Rotate(TrigAngle)
- sine = (SecondCornerY-FirstCornerY)/100
- cosine = (SecondCornerx-FirstCornerx)/100
- IF (cosine = 0) //the tangent is infinite here! Don't divide by zero.
- tangent = 999999
- ELSE
- tangent = sine/cosine
- ENDIF
-
- [200001 MP_InvTan]
- GlobalVars TrigAngle, Trig1X, Trig1Y, Trig2X, Trig2Y
- SpriteVars MP_dir2point MP_point1x MP_point1y MP_point2x MP_point2y MP_tx MP_p45 MP_Tinverted MP_Tdiffx MP_Tdiffy MP_Tanglesign MP_Tanglepolarity
- //This is the inverse tangent, basically a standard coordinate system arctangent
- MP_Tdiffx = (MP_point1x-MP_point2x)
- MP_Tdiffy = (MP_point1y-MP_point2y)
- MP_tx = ABS(MP_Tdiffx)/ ABS(MP_Tdiffy)
- MP_p45 = 0
- IF ( MP_tx > 0.65 AND MP_tx < 1.6)
- Stretch(MP_point1x,MP_point1y,MP_point2x,MP_point1y,MP_point2x,MP_point2y,MP_point1x,MP_point2y)
- Rotate(45)
- MP_Tdiffx = FirstCornerX - ThirdCornerX
- MP_Tdiffy = FirstCornerY - ThirdCornerY
- MP_p45 = -45
- ENDIF
-
-
- IF (ABS(MP_Tdiffx) < ABS(MP_Tdiffy))
- MP_Tinverted = 90
- MP_Tanglesign = -1
- MP_tx = MP_Tdiffx/MP_Tdiffy
- IF (MP_Tdiffy>1)
- MP_Tanglepolarity = 0
- ELSE
- MP_Tanglepolarity = 180
- ENDIF
- ELSE
- MP_Tanglesign = 1
- MP_Tinverted = 0
- MP_tx = MP_Tdiffy/MP_Tdiffx
- IF (MP_Tdiffx<1)
- MP_Tanglepolarity = 0
- ELSE
- MP_Tanglepolarity = 180
- ENDIF
- ENDIF
- //Above are all correction factors for turning a single quadrant function into a 4 quad function.
- //Below is the actual atan calculation as a series:
- MP_dir2point=MP_tx*(1+MP_tx*MP_tx*(-1/3+MP_tx*MP_tx*(1/5+MP_tx*MP_tx*(-1/7+MP_tx*MP_tx*(1/9-MP_tx*MP_tx/11)))))
- TrigAngle = MP_Tanglesign*(MP_dir2point*180/3.1415926 + MP_Tinverted) - MP_Tanglepolarity + MP_p45
-
-
- [200002 ArcTan]
- GlobalVars TrigInput, TrigOutput, TrigAngle
- SpriteVars MP_point1x MP_point1y MP_point2x MP_point2y MP_tx
- //Set up the tangent ratio
- MP_tx = TrigInput
- MP_point1x = -100
- MP_point1y = -100
- IF(ABS(MP_tx) < 1)
- MP_point2x = -100 - 100*MP_tx
- MP_point2y = -100 - 100
- ELSE
- MP_point2x = -100 -100
- MP_point2y = -100 - 100/MP_tx
- ENDIF
- ExecuteEvent(200001)//Calculate the inverse Tangent.
- TrigOutput = -(TrigAngle + 90) //correct for an inverted coordinate system.
-
- [200003 Cart2Polar]
- GlobalVars TrigAngle, TrigDistance, Trig1X, Trig1Y, Trig2X, Trig2Y
- SpriteVars MP_Tinverted, MP_Tdiffx, MP_Tdiffy, MP_Tanglesign, MP_Tanglepolarity, MP_point1x MP_point1y MP_point2x MP_point2y
- //This function returns the vector components (TrigAngle, TrigDistance) of
- //Trig2 relative to Trig1.
-
- MP_point1x = Trig1X
- MP_point1y = Trig1Y
- MP_point2x =Trig2X
- MP_point2y =Trig2Y
-
- ExecuteEvent(200001) //Calculate the inverse Tangent.
-
- IF (TrigAngle = -180)
- TrigDistance = Trig1X - Trig2X
- ELSE
- //Let the sprite calculate the squareroot for us since we know the angle already
- Stretch(Trig1X,Trig1Y,Trig2X,Trig1Y,Trig2X,Trig2Y,Trig1X,Trig2Y)
- Rotate(-TrigAngle)
- TrigDistance=BoundsRight-BoundsLeft
- ENDIF
-
-
- [200004 Polar2Cart]
- GlobalVars TrigAngle, TrigDistance, Trig1X, Trig1Y, sine, cosine
-
- ExecuteEvent(200000) //The Trig function
- Trig1X = TrigDistance * cosine
- Trig1Y = TrigDistance * sine
-
- [200014 Deg2Rad]
- GlobalVars TrigInput, TrigOutput
-
- TrigOutput = TrigInput * pi / 180
-
-
- [200005 Rad2Deg]
- GlobalVars TrigInput, TrigOutput
-
- TrigOutput = TrigInput * 180 / pi
-
-
- [200006 Sqrt]
- GlobalVars TrigInput, TrigOutput
- SpriteVars MP_tx, MP_rootscale
- //Squareroot function for LiveStage. Uses a folded series calculation developed by Matthew Peterson.
- MP_tx = ABS(TrigInput)
- MP_rootscale = 1
-
- IF (MP_tx = 0)
- TrigOutput = 0
- ELSE
- //only one WHILE will be called!
- WHILE (MP_tx < 0.4)
- MP_tx = MP_tx*4
- MP_rootscale = MP_rootscale/2
- ENDWHILE
-
- WHILE (MP_tx > 1.6)
- MP_tx = MP_tx/4
- MP_rootscale = MP_rootscale*2
- ENDWHILE
- MP_tx = MP_tx - 1
- TrigOutput = (1+MP_tx*(0.5+MP_tx*(-1/8+MP_tx*(1/16+MP_tx*(-5/128+0.02734*MP_tx)))))*MP_rootscale
- ENDIF
-
-
- [200007 Modulus]
- GlobalVars TrigInput TrigMod TrigOutput
- // The Modulus function. Performs TrigOutput = TrigInput MOD TrigMod
- // LiveStage only has a REM function. This is the same as a MOD function
- // for positive values, but differs for negative values. For instance,
- // -90 rem 360 = -90
- // -90 mod 360 = 270
- // For angles, the MOD function is what is needed.
- TrigOutput = (TrigMod+(TrigInput rem TrigMod)) rem TrigMod
-
-
-